package io.renren.common.utils.poi;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

/**
 * 单元格样式帮助类
 */
public class ExcelStyleHelper {

    /**
     *  设置单元格标题样式
     * @param workbook
     * @param tcell
     */
    public static void setTileCell(HSSFWorkbook workbook,HSSFCell tcell) {
        // 字段字体设置
        HSSFFont tfont = workbook.createFont();
        tfont.setFontName("黑体");
        tfont.setFontHeightInPoints((short) 11);
        tfont.setBold(true);   //字体加粗
        // 字段表格样式
        HSSFCellStyle tstyle = workbook.createCellStyle();
        tstyle.setFont(tfont);
        tstyle.setVerticalAlignment(VerticalAlignment.CENTER);  // 垂直居中
        tstyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); // 是设置前景色不是背景色
        tstyle .setFillPattern(FillPatternType.SOLID_FOREGROUND);
        tstyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        // 单元格设置样式
        tcell.setCellStyle(tstyle);
    }

    /**
     * 设置单元格左上
     * @param cell
     */
    public static void setCellLeftTop(HSSFCell cell) {
        HSSFCellStyle style = cell.getCellStyle();
        style.setAlignment(HorizontalAlignment.LEFT);
        style.setVerticalAlignment(VerticalAlignment.TOP);

        cell.setCellStyle(style);
    }

    /**
     * 设置单元格右上
     * @param cell
     */
    public static void setCellRightTop(HSSFCell cell) {
        HSSFCellStyle style = cell.getCellStyle();
        style.setAlignment(HorizontalAlignment.RIGHT);
        style.setVerticalAlignment(VerticalAlignment.TOP);

        cell.setCellStyle(style);
    }

    /**
     * 设置单元格多行换行
     * @param cell
     */
    public static void setCellWrap(HSSFCell cell) {
        HSSFCellStyle style = cell.getCellStyle();
        style.setWrapText(true);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        cell.setCellStyle(style);
    }

    /**
     * 设置单元格字体颜色
     * @param workbook
     * @param cell
     */
    public static void setFontColor(HSSFWorkbook workbook,HSSFCell cell,short fontcolor) {
        // 标红字体
        HSSFCellStyle rstyle = cell.getCellStyle();
        HSSFFont rfont = workbook.createFont();
        rfont.setColor(fontcolor);
        rstyle.setFont(rfont);
        cell.setCellStyle(rstyle);
    }
}
